package com.lehecai.core.search.api.impl;

import com.lehecai.core.search.api.SearchApiRequestAggregate;
import net.sf.json.JSON;
import net.sf.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

/**
 * Aggregate操作时的Body封装
 * User: sunshow
 * Date: 13-4-1
 * Time: 上午8:53
 */
public class AggregateSearchApiRequestBody extends AbstractSearchApiRequestBody {

    /**
     * 基础数据筛选
     */
    private SelectSearchApiRequestBody select;

    private List<SearchApiRequestAggregate> aggregateList = new ArrayList<SearchApiRequestAggregate>();

    @Override
    public JSON toJSON() {
        JSONArray jsonArray = new JSONArray();

        // select必须存在，此处不进行判空，编码时即暴露错误
        jsonArray.add(this.select.toJSON());

        // 聚合请求
        for (SearchApiRequestAggregate aggregate : this.aggregateList) {
            jsonArray.add(aggregate.toJSON());
        }

        return jsonArray;
    }

    public void addAggregate(SearchApiRequestAggregate aggregate) {
        this.aggregateList.add(aggregate);
    }

    public SelectSearchApiRequestBody getSelect() {
        return select;
    }

    public void setSelect(SelectSearchApiRequestBody select) {
        this.select = select;
    }
}
